/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.funda.xmppserver.handlers;

import com.funda.xmppserver.ComponentParser;
import com.funda.xmppserver.EventHandler;
import com.funda.xmppserver.XMPPClient;
import com.funda.xmppserver.XMPPInfo;
import com.funda.xmppserver.handlers.iqhandlers.IQBindHandler;
import com.funda.xmppserver.handlers.iqhandlers.IQQueryHandler;
import com.funda.xmppserver.handlers.iqhandlers.IQSessionHandler;
import com.funda.xmppserver.writer.XMPPPacketWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;

/**
 *
 * @author kurinchi
 */
public class IQHandler implements ComponentParser {

    XMPPPacketWriter packetwriter;
    XMPPClient client;
    Connection conn;
    Statement smt;
    ResultSet rs;
    Boolean valid = true;
    String id;
    String type;
    Map delegates = new HashMap();
    EventHandler iqhandler;

    public IQHandler(XMPPPacketWriter packetwriter, XMPPClient client) {
        this.packetwriter = packetwriter;
        this.client = client;

        iqhandler = new EventHandler(delegates);

        

    }

    public void parseElement(XMLStreamReader xmlReader) throws XMLStreamException {
        System.out.println("IQ Handler...");
        valid=true;
        for (int i = 0; i < xmlReader.getAttributeCount(); i++) {
            System.out.println(xmlReader.getAttributeLocalName(i) + " -> " + xmlReader.getAttributeValue(i));
            String attribname = xmlReader.getAttributeLocalName(i);
            String attribval = xmlReader.getAttributeValue(i);

            if (!(attribname.equalsIgnoreCase("id") || attribname.equalsIgnoreCase("type") || attribname.equalsIgnoreCase("to"))) {
                valid = false;
            }
            if (attribname.equalsIgnoreCase("id")) {
                id = attribval;
            }
            if (attribname.equalsIgnoreCase("type")) {
                type = attribval;
            }
            if (attribname.equalsIgnoreCase("to")) {
                if (!attribval.equalsIgnoreCase(XMPPInfo.getHOST())) {
                    valid = false;
                }
            }

        }

        System.out.println("IQ Result : " + valid);

        if(valid)
        {
            iqhandler.registerParser("bind", new IQBindHandler(packetwriter, client, id, type));
            iqhandler.registerParser("session", new IQSessionHandler(packetwriter, client, id, type));
            iqhandler.registerParser("query", new IQQueryHandler(packetwriter, client, id, type));
        }

        int event = xmlReader.getEventType();
        System.out.println(xmlReader.getLocalName());

        while (xmlReader.hasNext()) {
            xmlReader.next();
            event = xmlReader.getEventType();
            if (event == XMLEvent.START_ELEMENT) {
                System.out.println(xmlReader.getLocalName());
                String localname = xmlReader.getLocalName();
                if (delegates.containsKey(localname)) {
                    ComponentParser parser = (ComponentParser) delegates.get(localname);
                    parser.parseElement(xmlReader);
                }
            }
            if(event==XMLEvent.END_ELEMENT)
            {
                if(xmlReader.getLocalName().equalsIgnoreCase("iq"))
                {
                    break;
                }
            }
        }

    }
}
